<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板</title>
    <style>
        .container {
            width: 350px;
            height: 300px;
            margin: 0 auto;
            /* border: 1px black solid; */
            text-align: center;
        }

        .grey {
            color: grey;
        }

        .container .row {
            width: 350px;
            height: 40px;

            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .container .row input {
            width: 260px;
            height: 30px;
        }

        #submit {
            width: 350px;
            height: 40px;
            background-color: orange;
            color: white;
            border: none;
            margin: 10px;
            border-radius: 5px;
            font-size: 20px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>留言板</h1>
        <p class="grey">输入后点击提交, 会将信息显示下方空白处</p>
        <div class="row">
            <span>谁:</span> <input type="text" name="" id="from">
        </div>
        <div class="row">
            <span>对谁:</span> <input type="text" name="" id="to">
        </div>
        <div class="row">
            <span>说什么:</span> <input type="text" name="" id="say">
        </div>
        <input type="button" value="提交" id="submit" onclick="submit()">
        <!-- <div>A 对 B 说: hello</div> -->
    </div>

    <script src="js/jquery-3.7.1.min.js"></script>
    <script>
        // 对于存储的数据的打印
        function load() {
            $.ajax({
                url: "/message/getList",
                type: "get",
                //无参数不用传递
                success: function(messages) {
                    if (messages != null && messages.length > 0) {
                        // 进行打印
                        var fianlHTML = "";
                        for (var m of messages) {
                            fianlHTML += "<div>"+m.from +"对" + m.to + "说:" + m.message+"</div>";
                        }
                        $(".container").append(fianlHTML);
                    }
                }
            });
        }
    </script>
    <script>
        load();

        function submit(){
            //1. 获取留言的内容
            var from = $('#from').val();
            var to = $('#to').val();
            var say = $('#say').val();
            if (from== '' || to == '' || say == '') {
                return;
            }
            // 对于输入的数据存储
            var data = {
                from: from,
                to: to,
                message: say
            };
            // 还是使用 ajax
            $.ajax({
                url: "/message/publish",
                type: "post",
                contentType: "application/json",
                // JSON.stringify()  将对象转换成 json
                data: JSON.stringify(data),
                //等待后端代码的执行结束
                success: function(ret) {
                    // 把接收到的 json 对象类型转换成 对象
                    var jsonObj = JSON.parse(ret);
                    // 对于 json 来说可以对其直接调用其中的属性
                    if (jsonObj.ok == 1) {
                        // 存储成功，进行输出
                        //2. 构造节点
                        var divE = "<div>"+from +"对" + to + "说:" + say+"</div>";
                        //3. 把节点添加到页面上    
                        $(".container").append(divE);

                        //4. 清空输入框的值
                       $('#from').val("");
                       $('#to').val("");
                       $('#say').val("");
                    }else {
                        //失败
                        alert("发布留言失败");
                    }
                }
            });
            
        }  
    </script>
    <script>
        // 对于存储的数据的打印
        function load() {
            $.ajax({
                url: "/message/getList",
                type: "get",
                //无参数不用传递
                success: function(messages) {
                    if (messages != null && messages.length > 0) {
                        // 进行打印
                        var fianlHTML = "";
                        for (var m of messages) {
                            fianlHTML += "<div>"+m.from +"对" + m.to + "说:" + m.message+"</div>";
                        }
                        $(".container").append(fianlHTML);
                    }
                }
            });
        }
    </script>
</body>

</html>